<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    PHP 适配器模式(Adapter Design Pattern) |
    
    无敌小笼包</title>
  
    <link rel="shortcut icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
  
  <script src="/js/pace.min.js"></script>
</head>

<body>
<main class="content">
  <section class="outer">
  <article id="post-phpAdapter" class="article article-type-post" itemscope itemprop="blogPost">

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      PHP 适配器模式(Adapter Design Pattern)
    </h1>
  

      </header>
    

    
      <div class="article-meta">
        <a href="/2017/02/21/phpAdapter/" class="article-date">
  <time datetime="2017-02-21T02:20:12.000Z" itemprop="datePublished">2017-02-21</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/Skill/">Skill</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      
      
      
        <ul>
<li>适配器设计模式知识将某个对象的借口是配为另一个对象所期望的接口</li>
</ul>
<p>通过适配器模式能够使用新的代码和功能性来帮助更新原有的系统，简单的说需要转化一个对象的接口用于另一个对象中时，在不改变原对象的基础上可以采用适配器模式</p>
<a id="more"></a>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Adapter  适配器模式</div><div class="line"> * -------------------------------------</div><div class="line"> * ** 来自说明 **</div><div class="line"> * </div><div class="line"> * 通过适配器模式能够使用新的代码和功能性来帮助更新原有的系统。</div><div class="line"> * 简单的说需要转化一个对象的接口用于另一个对象中时，</div><div class="line"> * 在不改变原对象的基础上可以采用适配器模式</div><div class="line"> * </div><div class="line"> * ===================================== </div><div class="line"> * ** 应用场景 **</div><div class="line"> *</div><div class="line"> * 对于项目中比较旧的架构底层的基类做接口更改时使用 </div><div class="line"> * </div><div class="line"> * -------------------------------------</div><div class="line"> * </div><div class="line"> * <span class="doctag">@version</span> $&#123;Id&#125;$</div><div class="line"> * <span class="doctag">@author</span> Shaowei Pu &lt;542684913<span class="doctag">@QQ</span>.com&gt;</div><div class="line"> */</div></pre></td></tr></table></figure>
<hr>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"><span class="comment">// 你现在是大巍施工队分配到伊朗的挖矿工头。</span></div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 大巍施工队总部</div><div class="line"> */</div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">headquarters</span>  </span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">protected</span> $_worker;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">($worker)</span></span>&#123;</div><div class="line">        <span class="keyword">$this</span>-&gt;_worker = $worker;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getWorker</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">if</span>( !<span class="keyword">empty</span>(<span class="keyword">$this</span>-&gt;_worker) )&#123;</div><div class="line">            <span class="keyword">return</span> <span class="string">'力大无穷'</span>.<span class="keyword">$this</span>-&gt;_worker.<span class="string">'个挖矿师傅'</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">/**</span></div><div class="line"> * 分包出去干活的</div><div class="line"> */</div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">assignWorker</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">protected</span> $_workerObject;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">(headquarters $_workerObject )</span></span>&#123;</div><div class="line">        <span class="keyword">$this</span>-&gt;_workerObject = $_workerObject;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">assign</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">echo</span> <span class="keyword">$this</span>-&gt;_workerObject-&gt;getWorker(); <span class="comment">// 将错误信息输出至控制台</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 现在你要20个力大无穷的搬砖师傅，然后找下总部，总部就给你了</div><div class="line"> */</div><div class="line">$Iran   = <span class="keyword">new</span> assignWorker(<span class="keyword">new</span> headquarters(<span class="number">20</span>));</div><div class="line">$Iran-&gt;assign();</div><div class="line"><span class="keyword">echo</span> <span class="string">"&lt;hr/&gt;"</span>;</div><div class="line"></div><div class="line"><span class="comment">// 日复一日，年复一年，你又被调到黎巴嫩挖矿，这边不同于你在伊朗的日子了，这里你要挖取钻石矿</span></div><div class="line"><span class="comment">// 但是这个钻石矿要几个砖家配合挖矿师傅才行，可是公司现在不能提供给你其他部门，因为咱们是百年大企业，主单位不能随便加部门进去</span></div><div class="line"><span class="comment">// 大巍国际的人才培养很利索，要什么有什么，这可难不倒你。所以你想了这样一个办法</span></div><div class="line"><span class="comment">// </span></div><div class="line"><span class="comment">/**</span></div><div class="line"> * 黎巴嫩专用适配器</div><div class="line"> */</div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Adapter</span> <span class="keyword">extends</span> <span class="title">headquarters</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">($worker)</span></span>&#123;</div><div class="line">        <span class="keyword">parent</span>::__construct($worker);</div><div class="line">        <span class="keyword">$this</span>-&gt;getallheaders();</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getallheaders</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">$this</span>-&gt;_worker =<span class="string">'勇敢过人的'</span>.(<span class="keyword">$this</span>-&gt;_worker/<span class="number">2</span>).<span class="string">'个砖家在加上'</span>.<span class="keyword">$this</span>-&gt;_worker;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">// 这下你要的的人就出来了</span></div><div class="line">$Lebanon = <span class="keyword">new</span> assignWorker(<span class="keyword">new</span> Adapter(<span class="number">20</span>));</div><div class="line">$Lebanon-&gt;assign();</div></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://blog.mango16.cc/2017/02/21/phpAdapter/" data-id="cjrbtw1y3000z6d8hy4bhtint"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Design-Pattern/">Design Pattern</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2017/02/22/Builder/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            PHP Builder 建造者模式
          
        </div>
      </a>
    
    
      <a href="/2017/02/20/hiderjs/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">javascript 之隐藏你的代码</div>
      </a>
    
  </nav>


  

  
    
  <div class="gitalk" id="gitalk-container"></div>
  <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
  <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
  <script type="text/javascript">
      var gitalk = new Gitalk({
        clientID: 'caba8826a61790936307',
        clientSecret: '09a15037dcab30d9adf35a53ec6961939169f797',
        repo: 'm9rco.github.io',
        owner: 'm9rco',
        admin: ['m9rco'],
        // id: location.pathname,      // Ensure uniqueness and length less than 50
        id: md5(location.pathname),
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

  gitalk.render('gitalk-container')
  </script>

  

</article>



</section>
  <footer class="footer">
  
  <div class="outer">
    <ul class="list-inline">
      <li>&copy; 2019 无敌小笼包</li>
      <i class="pulse"></i>
    </ul>
  </div>
</footer>

</main>
<aside class="sidebar">
  <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/hexo.svg" alt="无敌小笼包"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">相册</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/nagging">叽咕</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="Search">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
</aside>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

</body>
</html>